var rootPath = getRootPath();

$(function() {
	$('.skin-minimal input').iCheck({
		checkboxClass : 'icheckbox-blue',
		radioClass : 'iradio-blue',
		increaseArea : '20%'
	});

	jQuery.validator.addMethod("checkUname", function(value, element) {
		var oriUname = $("#oriUname").val();
		return checkLogin(value, oriUname);
	}, "昵称已存在");
	
	jQuery.validator.addMethod("isRightPhone", function(value, element) {
		return isRightPhone(value);
	}, "手机号码有误");
	
	jQuery.validator.addMethod("checkPhone", function(value, element) {
		var oriPhone = $("#oriPhone").val();
		return checkLogin(value, oriPhone);
	}, "手机号码已存在");

	jQuery.validator.addMethod("checkEmail", function(value, element) {
		var oriEmail = $("#oriEmail").val();
		return checkLogin(value, oriEmail);
	}, "电子邮箱已存在");

	$("#form-user-change-info").validate({
		rules : {
			uname : {
				required : true,
				minlength : 1,
				maxlength : 16,
				checkUname : true
			},
			phone : {
//				isPhone : true,    //检测可能出错
				isRightPhone : true,
				checkPhone : true
			},
			email : {
				email : true,
				checkEmail : true
			}
		},
		onkeyup : false,
		focusCleanup : true,
		success : "valid",
		submitHandler : function(form) {
			$.ajax({
				url : rootPath + '/user/updateUserLogin',
				type : 'POST',
				data : JSON.stringify($('#form-user-change-info').serializeJson()),
				contentType : 'application/json;charset=UTF-8',
				success : function(data) {
					if("0000" == data.returnCode) {						
						layer.msg('个人信息修改成功!', {icon : 1,time : 1000}, function() {
							parent.window.location.href = rootPath + '/manager';
							var index = parent.layer.getFrameIndex(window.name);
							parent.$('.btn-refresh').click();
							parent.layer.close(index);
						});
					} else {
						layer.alert(data.returnMessage);
					}
				},
				error : function(jqXHR, textStatus, errorThrown) {
					layer.msg('error:)', {icon : 1,time : 1000});
				}
			});
		}
	});

	$("#form-user-change-pwd").validate({
		rules : {
			oriPwd : {
				required : true,
				remote : {
					type : "GET",
					url : rootPath + "/login/checkPassword",
					data : {
						uid : function() {
							return $("#uid").val();
						},
						password : function() {
							return $("#oriPwd").val();
						}
					},
					dataFilter : function(data, type) {// 判断控制器返回的内容
						if (data == "true") {
							return true;
						} else {
							return false;
						}
					}
				}
			},
			password : {
				required : true,
			},
			password2 : {
				required : true,
				equalTo : "#password"
			}
		},
		messages : {
			oriPwd : {
				remote : "原始密码输入有误"
			}
		},
		onkeyup : false,
		focusCleanup : true,
		success : "valid",
		submitHandler : function(form) {
			$.ajax({
				url : rootPath + '/personal/changePersonalPwd',
				type : 'POST',
				data : JSON.stringify($('#form-user-change-pwd').serializeJson()),
				contentType : 'application/json;charset=UTF-8',
				success : function(data) {
					if("0000" == data.returnCode) {
						layer.msg('密码修改成功', {icon : 1,time : 1000}, function() {
							parent.window.location.href = rootPath + '/manager';
							var index = parent.layer.getFrameIndex(window.name);
							parent.$('.btn-refresh').click();
							parent.layer.close(index);
						});
					} else {
						layer.alert(data.returnMessage);
					}
				},
				error : function(jqXHR, textStatus, errorThrown) {
					layer.msg('error:)', {
						icon : 1,
						time : 1000
					});
				}
			});
		}
	});
});

/**
 * 检测 login是否有效
 * 
 * @param login
 *            填入的login
 * @param oriLogin
 *            原始的login
 * @returns {Boolean}
 */
function checkLogin(login, oriLogin) {
	if(login == '') {
		return true;
	}
	var isValidLogin = true;
	if (login != oriLogin) {
		$.ajax({
			url : rootPath + '/login/checkLogin',
			type : 'GET',
			async : false,
			data : {
				"login" : login
			},
			success : function(data) {
				if (data != "true") {
					isValidLogin = false;
				}
			},
			error : function(jqXHR, textStatus, errorThrown) {
				layer.alert("error:(");
			}
		});
	}
	return isValidLogin;
}

function isRightPhone(phone) {
	if(phone == '') {
		return true;
	}
	var reg = /^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/;
	return reg.test(phone);
}